package com.scm.biz.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.scm.biz.bean.OrderPattern;
import com.scm.biz.enums.FileCategoryEnum;
import com.scm.biz.result.OrderPatternResult;
import com.scm.biz.service.BusinessFileService;
import com.scm.biz.service.OrderPatternService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.core.bean.resp.RespBean;
import com.scm.utils.UfdmDateUtil;

@RequestMapping("/orderpattern")
@Controller
public class OrderPatternAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	OrderPatternService orderPatternService;
	@Resource
	private BusinessFileService businessFileService;

	@RequestMapping("/insert")
	@ResponseBody
	public RespBean<String> insert(OrderPattern pattern,MultipartHttpServletRequest req)throws Exception{
		RespBean<String> respBean=new RespBean<String>();
		if(pattern.getPeriod()==null||pattern.getPeriod().trim().isEmpty()){
			throw new UfdmException("未选择年份");
		}
		if(pattern.getSeason()==null||pattern.getSeason().trim().isEmpty()){
			throw new UfdmException("未选择季节");
		}
		if(pattern.getSex()==null||pattern.getSex().trim().isEmpty()){
			throw new UfdmException("未选择性别");
		}
		orderPatternService.save(pattern,req);
		respBean.setBody("success");
		return respBean;
	}
	
	@RequestMapping("/addPage")
	public ModelAndView addPage(Integer businessId,Integer orderId){
		
		ModelAndView mv = new ModelAndView("order/upload_design");
		mv.addObject("businessId", businessId);
		mv.addObject("orderId", orderId);
		List<String[]> years = new ArrayList<String[]>();
		for(int i=5;i>0;i--){
			years.add(new String[]{UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), -i), "yyyy"),UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), -i), "yyyy")});
		}
		for(int i=0;i<5;i++){
			years.add(new String[]{UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), i), "yyyy"),UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), i), "yyyy")});
		}
		mv.addObject("years", years);
		return mv;
	}
	
	/**
	 * 设计款详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/detail")
	public ModelAndView detail(Long id,Long proofingOrderId,Long productionOrderId){
		ModelAndView mv=new ModelAndView("order/sub_order_detail");
		OrderPatternResult orderPattern=orderPatternService.getById(id);
		mv.addObject("orderPattern", orderPattern);
		mv.addObject("proofingOrderId", proofingOrderId);
		mv.addObject("productionOrderId", productionOrderId);
	
		return mv;
	}
	
	/**
	 * 显示设计稿修改界面
	 * @return
	 */
	@RequestMapping("/showEditPage")
	public ModelAndView showEditPage(Long id,Long proofingOrderId){
		ModelAndView mv=new ModelAndView("order/design_order_pattern_edit");
		OrderPatternResult pattern=orderPatternService.getById(id);
		List<String[]> years = new ArrayList<String[]>();
		for(int i=5;i>0;i--){
			years.add(new String[]{UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), -i), "yyyy"),UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), -i), "yyyy")});
		}
		for(int i=0;i<5;i++){
			years.add(new String[]{UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), i), "yyyy"),UfdmDateUtil.dateToString(UfdmDateUtil.getAfterDateByYears(new Date(), i), "yyyy")});
		}
		mv.addObject("years", years);
		mv.addObject("pattern", pattern);
		mv.addObject("proofingOrderId", proofingOrderId);
		return mv;
	}
	
	/**
	 * 修改订单款式
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public RespBean<String> doEdit(OrderPattern pattern,MultipartHttpServletRequest req) throws Exception{
		RespBean<String> respBean=new RespBean<String>();
		if(pattern.getPeriod()==null||pattern.getPeriod().trim().isEmpty()){
			throw new UfdmException("未选择年份");
		}
		if(pattern.getSeason()==null||pattern.getSeason().trim().isEmpty()){
			throw new UfdmException("未选择季节");
		}
		if(pattern.getSex()==null||pattern.getSex().trim().isEmpty()){
			throw new UfdmException("未选择性别");
		}
		orderPatternService.modify(pattern,req);
		return respBean;
	}
	
	/**
	 * 获取设计稿的图片
	 * @param orderPatternId
	 * @return
	 */
	@RequestMapping("/showImage")
	@ResponseBody
	public RespBean<List<String>> showImage(Long orderPatternId){
		RespBean<List<String>> respBean=new RespBean<List<String>>();
		List<String> list=businessFileService.getImage(orderPatternId, FileCategoryEnum.ORDER_PATTERN_IMAGE.value());
		respBean.setBody(list);
		return respBean;
	}
}
